#include "bsp_keyscan.h"
#include "bsp_interrupt.h"
/**
 *******************************************************************************
 ** \brief ExtInt12 as key row 0 callback function
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void KeyRow0_Callback(void)
{
    if (Set == EXINT_IrqFlgGet(ExtiCh12))
    {
        /* clear int request flag */
        EXINT_IrqFlgClr(ExtiCh12);
    }
}

/**
 *******************************************************************************
 ** \brief ExtInt13 as key row 1 callback function
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void KeyRow1_Callback(void)
{
    if (Set == EXINT_IrqFlgGet(ExtiCh13))
    {
        /* clear int request flag */
        EXINT_IrqFlgClr(ExtiCh13);
    }
}

/**
 *******************************************************************************
 ** \brief ExtInt14 as key row 2 callback function
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void KeyRow2_Callback(void)
{
    if (Set == EXINT_IrqFlgGet(ExtiCh14))
    {
        /* clear int request flag */
        EXINT_IrqFlgClr(ExtiCh14);
    }
}


/**
 *******************************************************************************
 ** \brief Key row 0 init function
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void KeyRow0_Init(void)
{
    stc_exint_config_t stcExtiConfig;
    stc_port_init_t stcPortInit;

    /* configuration structure initialization */
    MEM_ZERO_STRUCT(stcExtiConfig);
    MEM_ZERO_STRUCT(stcPortInit);

    /**************************************************************************/
    /* External Int Ch.12 for keyin0                                          */
    /**************************************************************************/
    stcExtiConfig.enExitCh = ExtiCh12;

    /* Filter setting */
    stcExtiConfig.enFilterEn = Enable;
    stcExtiConfig.enFltClk = Pclk3Div8;
    /* falling edge for keyscan function */
    stcExtiConfig.enExtiLvl = ExIntFallingEdge;
    EXINT_Init(&stcExtiConfig);

    /* Set PD12 as External Int Ch.12 input */
    MEM_ZERO_STRUCT(stcPortInit);
    stcPortInit.enExInt = Enable;
    stcPortInit.enPullUp = Enable;
    PORT_Init(KEYIN_PORT, KEYIN0_PIN, &stcPortInit);
    bsp_interrupt_callback_regist(INT_PORT_EIRQ12, KEY_ROW0_IRQn, KeyRow0_Callback);
}

/**
 *******************************************************************************
 ** \brief Key row 1 init function
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void KeyRow1_Init(void)
{
    stc_exint_config_t stcExtiConfig;
    stc_port_init_t stcPortInit;

    /* configuration structure initialization */
    MEM_ZERO_STRUCT(stcExtiConfig);
    MEM_ZERO_STRUCT(stcPortInit);

    /**************************************************************************/
    /* External Int Ch.13 for keyin1                                          */
    /**************************************************************************/
    stcExtiConfig.enExitCh = ExtiCh13;

    /* Filter setting */
    stcExtiConfig.enFilterEn = Enable;
    stcExtiConfig.enFltClk = Pclk3Div8;
    /* falling edge for keyscan function */
    stcExtiConfig.enExtiLvl = ExIntFallingEdge;
    EXINT_Init(&stcExtiConfig);

    /* Set PD13 as External Int Ch.13 input */
    MEM_ZERO_STRUCT(stcPortInit);
    stcPortInit.enExInt = Enable;
    stcPortInit.enPullUp = Enable;
    PORT_Init(KEYIN_PORT, KEYIN1_PIN, &stcPortInit);
    
    bsp_interrupt_callback_regist(INT_PORT_EIRQ13, KEY_ROW1_IRQn, KeyRow1_Callback);
}

/**
 *******************************************************************************
 ** \brief Key row 2 init function
 **
 ** \param  None
 **
 ** \retval None
 **
 ******************************************************************************/
void KeyRow2_Init(void)
{
    stc_exint_config_t stcExtiConfig;
    stc_port_init_t stcPortInit;

    /* configuration structure initialization */
    MEM_ZERO_STRUCT(stcExtiConfig);
    MEM_ZERO_STRUCT(stcPortInit);

    /**************************************************************************/
    /* External Int Ch.14 for keyin2                                          */
    /**************************************************************************/
    stcExtiConfig.enExitCh = ExtiCh14;

    /* Filter setting */
    stcExtiConfig.enFilterEn = Enable;
    stcExtiConfig.enFltClk = Pclk3Div8;
    /* falling edge for keyscan function */
    stcExtiConfig.enExtiLvl = ExIntFallingEdge;
    EXINT_Init(&stcExtiConfig);

    /* Set PD14 as External Int Ch.14 input */
    MEM_ZERO_STRUCT(stcPortInit);
    stcPortInit.enExInt = Enable;
    stcPortInit.enPullUp = Enable;
    PORT_Init(KEYIN_PORT, KEYIN2_PIN, &stcPortInit);

    bsp_interrupt_callback_regist(INT_PORT_EIRQ14, KEY_ROW2_IRQn, KeyRow2_Callback);
}

void keyscan_col_init(void)
{
    stc_port_init_t stcPortInit;
    stc_keyscan_config_t stcKeyscanConfig;

    /* configuration structure initialization */
    MEM_ZERO_STRUCT(stcPortInit);

    /* enable internal pull-up */
    //stcPortInit.enPullUp = Enable;
    PORT_Init(KEYOUT_PORT, (KEYOUT0_PIN | KEYOUT1_PIN | KEYOUT2_PIN), &stcPortInit);

    /* Set corresponding pins to KEYSCAN function */
    PORT_SetFunc(KEYOUT_PORT, (KEYOUT0_PIN | KEYOUT1_PIN | KEYOUT2_PIN), Func_Key, Disable);

    /* enable KEYSCAN module source clock */
    PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_KEY, Enable);

    /* output Hiz state 512 clocks*/
    stcKeyscanConfig.enHizCycle = Hiz512;

    /* output Low state 512 clocks */
    stcKeyscanConfig.enLowCycle = Low512;

    /* use HCLK as scan clock */
    stcKeyscanConfig.enKeyscanClk = KeyscanHclk;

    /* KEYOUT 0~3 are selected as column */
    stcKeyscanConfig.enKeyoutSel = Keyout0To2;

    /* KEYIN 12~14 are selected as row */
    stcKeyscanConfig.u16KeyinSel = (Keyin12 | Keyin13 | Keyin14);

    KEYSCAN_Init(&stcKeyscanConfig);
    
}


void bsp_keyscan_init(void)
{
    KeyRow0_Init();
    KeyRow1_Init();
    KeyRow2_Init();
    keyscan_col_init();
}






